<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom: 10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changFn"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :data="depts"
          default-expand-all
          :expand-on-click-node="false"
          :highlight-current="true"
          :props="defaultProps"
          node-key="id"
          @node-click="handleNodeClick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="tableData">
          <el-table-column align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar
                v-if="row.staffPhoto"
                :size="30"
                :src="row.staffPhoto"
              />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column label="聘用形式">
            <template v-slot="{ row }">
              <span>{{ employmentType[row.formOfEmployment] || "无" }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{row}">
              <el-button size="mini" type="text">查看</el-button>
              <el-button size="mini" type="text">角色</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="delConfirm(row.id)"
              >
                <template #reference>
                  <el-button size="mini" type="text">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page.sync="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="getEmployeeList"
          />
        </el-row>
      </div>
    </div>
    <ImportExcel :show-excel-dialog.sync="showExcelDialog" />
  </div>
</template>

<script>
import { transListToTree } from '@/utils'
import { getEmployeeList, exportEmployee, delEmployee } from '@/api/employye'
import { getDepartment } from '@/api/department'
import FileSave from 'file-saver'
import ImportExcel from './components/ImportExcel.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      showExcelDialog: false,
      depts: [],
      tableData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 请求员工列表参数
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 5,
        keyword: ''
      },
      total: 0,
      employmentType: {
        1: '正式',
        2: '非正式'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async delConfirm(id) {
      await delEmployee(id)
      this.$message.success('删除成功')
      if (this.tableData.length === 1 && this.queryParams.page > 1) this.queryParams.page--
      this.getEmployeeList()
    },
    async exportEmployee() {
      const res = await exportEmployee()
      FileSave.saveAs(res, '员工信息表.xlsx')
    },
    changFn() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeList()
      }, 500)
    },
    handleNodeClick(current) {
      this.queryParams.departmentId = current.id
      this.queryParams.page = 1
      this.getEmployeeList()
    },
    async getDepartment() {
      const res = await getDepartment()
      this.depts = transListToTree(res, 0)
      // console.log(res)
      this.queryParams.departmentId = this.depts[0].id
      // 因为vue更新dom是异步的 所以我们要在tree更新之后设置选中节点
      this.$nextTick(() => {
        // 设置节点选中状态
        this.$refs.tree.setCurrentKey(this.queryParams.departmentId)
      })
      this.getEmployeeList()
    },
    async getEmployeeList() {
      const { total, rows } = await getEmployeeList({ ...this.queryParams })
      this.tableData = rows
      this.total = total
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>
